package 二叉树;

import common.TreeNode;

/**
 * @author tmh
 * @date 2024/8/21 22:09
 * @description
 */
public class T101对称二叉树 {
    /**
     * 内外层得思想
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        return compare(root.left,root.right);
    }

    public boolean compare(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        //如果左子树为空，右子树不为空，返回false
        if (left == null && right != null) {
            return false;
        }
        //如果右子树为空，左子树不为空，返回false
        if (left != null && right == null) {
            return false;
        }
        //如果左右子树的值不相同，那么也返回false
        if (left.val != right.val) {
            return false;
        }
        //开始递归 外层 内层
        boolean outside = compare(left.left, right.right);
        boolean inside = compare(left.right, right.left);
        return outside && inside;
    }
}
